package view;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import net.miginfocom.swing.MigLayout;
import dto.BookDTO;
import dto.BorrowBookDTO;
import dto.LendBookDTO;
import dto.LogonDTO;

public class LendBookPage extends JDialog {

	private static final long serialVersionUID = 1L;

	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	JComboBox comboBoxLendingDays;

	private JLabel labelMessage;
	private JTextField textFieldToday;
	private JTextField textFieldTitle;
	private JTextField textFieldAuthor;
	private JTextField textFieldPublisher;
	private JTextField textFieldPublishedDate;
	private JTextField textFieldISBN;
	private JTextField textFieldCatalog;
	private JTextField textFieldDueDate = new JTextField();
	private JTextArea textAreaRemarks;
	private JTextArea textAreaNotes;
	private JTextField textFieldUsername;
	private JTextField textFieldLevel;

	private JButton btnLendBook = new JButton("Lend");
	private JButton btnCancel = new JButton("Cancel");

	private LendBookDTO lendBookDTO;

	private Date dateToday;
	private Date dueDate;

	public LendBookDTO getLendBookDTO() {
		lendBookDTO.setDeweyDecimalNo(textFieldCatalog.getText().trim());
		lendBookDTO.setLoanDate(dateToday);
		lendBookDTO.setDueDate(dueDate);
		lendBookDTO.setRemarks(textAreaRemarks.getText().trim());

		return lendBookDTO;
	}

	/**
	 * Create the dialog.
	 */
	public LendBookPage(LogonDTO logonDTO, BorrowBookDTO borrowDTO) {

		BookDTO bookDTO = borrowDTO.getBookDTO();

		lendBookDTO = new LendBookDTO();
		lendBookDTO.setBorrowerId(borrowDTO.getBorrowerId());

		setBounds(100, 100, 571, 468);
		getContentPane()
				.setLayout(
						new MigLayout(
								"",
								"[90.00,grow][grow][61.00,grow][grow][83.00,grow][grow]",
								"[][][][][][][][][][][][][grow][][][][][]"));

		JLabel lblJaLibrary = new JLabel("J&A Library");
		lblJaLibrary.setFont(new Font("Arial", Font.BOLD, 16));
		lblJaLibrary.setForeground(Color.BLUE);
		getContentPane().add(lblJaLibrary, "cell 0 0 6 1,alignx center");

		JLabel lblLendBook = new JLabel("Lend Book");
		getContentPane().add(lblLendBook, "cell 0 1 6 1,alignx center");

		JLabel label = new JLabel("     ");
		getContentPane().add(label, "cell 0 2");

		JLabel lblTodayIs = new JLabel("Today is:");
		getContentPane().add(lblTodayIs, "cell 4 2,alignx trailing");

		textFieldToday = new JTextField();
		textFieldToday.setHorizontalAlignment(SwingConstants.CENTER);
		textFieldToday.setEditable(false);
		getContentPane().add(textFieldToday, "cell 5 2,growx");
		textFieldToday.setColumns(10);

		JLabel lblTitle = new JLabel("Title:");
		getContentPane().add(lblTitle, "cell 0 4,alignx trailing");

		textFieldTitle = new JTextField();
		textFieldTitle.setEditable(false);
		textFieldTitle.setText(bookDTO.getTitle());
		getContentPane().add(textFieldTitle, "cell 1 4 4 1,growx");
		textFieldTitle.setColumns(10);

		JLabel lblAuthor = new JLabel("Author:");
		getContentPane().add(lblAuthor, "cell 0 5,alignx trailing");

		textFieldAuthor = new JTextField();
		textFieldAuthor.setEditable(false);
		textFieldAuthor.setText(bookDTO.getAuthor());
		getContentPane().add(textFieldAuthor, "cell 1 5 4 1,growx");
		textFieldAuthor.setColumns(10);

		JLabel lblPublisher = new JLabel("Publisher:");
		getContentPane().add(lblPublisher, "cell 0 6,alignx trailing");

		textFieldPublisher = new JTextField();
		textFieldPublisher.setEditable(false);
		textFieldPublisher.setText(bookDTO.getPublisher());
		getContentPane().add(textFieldPublisher, "cell 1 6 2 1,growx");
		textFieldPublisher.setColumns(10);

		JLabel lblPublishedDate = new JLabel("Published Date:");
		getContentPane().add(lblPublishedDate, "cell 3 6,alignx trailing");

		textFieldPublishedDate = new JTextField();
		textFieldPublishedDate.setEditable(false);
		textFieldPublishedDate.setText(bookDTO.getPublishedDate());
		textFieldPublishedDate.setColumns(10);
		getContentPane().add(textFieldPublishedDate, "cell 4 6,growx");

		JLabel lblIsbn = new JLabel("ISBN:");
		getContentPane().add(lblIsbn, "cell 0 7,alignx trailing");

		textFieldISBN = new JTextField();
		textFieldISBN.setEditable(false);
		textFieldISBN.setText(bookDTO.getISBN());
		getContentPane().add(textFieldISBN, "cell 1 7 3 1,growx");
		textFieldISBN.setColumns(10);

		JLabel lblCatalogNumberddc = new JLabel("Catalog (DDC):");
		getContentPane().add(lblCatalogNumberddc, "cell 0 8,alignx trailing");

		textFieldCatalog = new JTextField();
		textFieldCatalog.setEditable(false);
		textFieldCatalog.setText(bookDTO.getDeweyDecimalNo());
		getContentPane().add(textFieldCatalog, "cell 1 8 3 1,growx");
		textFieldCatalog.setColumns(10);

		JLabel lblDueDate = new JLabel("Lending Days:");
		getContentPane().add(lblDueDate, "cell 0 9,alignx trailing");

		comboBoxLendingDays = new JComboBox();
		comboBoxLendingDays.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent arg0) {
				Calendar calendar = Calendar.getInstance();
				String date = dateFormat.format(calendar.getTime());
				int adjustdays = (comboBoxLendingDays.getSelectedIndex() + 1);
				calendar.add(Calendar.DAY_OF_MONTH, adjustdays);
				date = dateFormat.format(calendar.getTime());

				textFieldDueDate.setText("" + date);
			}
		});
		comboBoxLendingDays.setModel(new DefaultComboBoxModel(new String[] {
				"1", "2", "3", "4", "5", "6", "7 (1 week)", "8", "9", "10",
				"11", "12", "13", "14 (2 weeks)", "15", "16", "17", "18", "19",
				"20", "21 (3 weeks)", "22", "23", "24", "25", "26", "27", "28",
				"29", "30 (1 month)" }));
		comboBoxLendingDays.setSelectedIndex(6);
		getContentPane().add(comboBoxLendingDays, "cell 1 9,alignx center");

		JLabel lblDate = new JLabel("Date:");
		getContentPane().add(lblDate, "cell 2 9,alignx trailing");

		{
			Calendar calendar = Calendar.getInstance();
			String date = dateFormat.format(calendar.getTime());
			int adjustdays = (comboBoxLendingDays.getSelectedIndex() + 1);
			calendar.add(Calendar.DAY_OF_MONTH, adjustdays);

			dueDate = calendar.getTime();
			date = dateFormat.format(dueDate);

			textFieldDueDate.setText(date);
			textFieldDueDate.setEditable(false);
			getContentPane().add(textFieldDueDate, "cell 3 9,growx");
			textFieldDueDate.setColumns(10);
		}

		labelMessage = new JLabel("     ");
		labelMessage.setForeground(Color.BLUE);
		getContentPane().add(labelMessage, "flowx,cell 0 16 6 1");

		{
			textAreaRemarks = new JTextArea();
			textAreaRemarks.setDocument(new JTextFieldLimit(1000));
			if ("N".equalsIgnoreCase(bookDTO.getOnLoan())) {
				textAreaRemarks.setEnabled(true);
				btnLendBook.setEnabled(true);

			} else if ("Y".equalsIgnoreCase(bookDTO.getOnLoan())) {
				// TODO instead of remarks, use notes that shows due date of
				// book, and borrower
				textAreaRemarks.setEnabled(true);
				btnLendBook.setEnabled(false);
			}
		}

		JLabel lblRemarks = new JLabel("Remarks:");
		getContentPane().add(lblRemarks, "cell 0 10,alignx trailing");

		getContentPane().add(textAreaRemarks, "cell 1 10 4 3,grow");

		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});

		getContentPane().add(btnLendBook, "cell 0 15,growx");
		getContentPane().add(btnCancel, "cell 5 15,growx");

		textFieldUsername = new JTextField();
		textFieldUsername.setEditable(false);
		textFieldUsername.setText(logonDTO.getUserName());
		getContentPane().add(textFieldUsername, "cell 0 17 2 1,growx");
		textFieldUsername.setColumns(10);

		textFieldLevel = new JTextField();
		textFieldLevel.setEditable(false);
		textFieldLevel.setText(logonDTO.getUserTitle());
		getContentPane().add(textFieldLevel, "cell 4 17 2 1,growx");
		textFieldLevel.setColumns(10);

		{

			Calendar calendar = Calendar.getInstance();
			dateToday = calendar.getTime();
			textFieldToday.setText(dateFormat.format(dateToday));

			labelMessage = new JLabel("     ");
			labelMessage.setForeground(Color.BLUE);
			getContentPane().add(labelMessage, "cell 0 16 6 1");
		}
	}

	public void addLendBookButtonListener(ActionListener al) {
		btnLendBook.addActionListener(al);
	}

	public void displayMessage(String message) {
		labelMessage.setText(message);
		btnLendBook.setEnabled(false);
		textAreaRemarks.setEnabled(false);
	}

}
